package ACWing.SearchAndGraphTheory.DFS;
//51.数字排列
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @author :chenjie
 * @date :Created 2022/12/18 20:04
 */
public class NumberArrangement {
    static List<Integer>list1=new ArrayList<>();
    static List<List<Integer>> list=new ArrayList<>();
    static boolean[]arr=new boolean[6];
    public static void main(String[] args) {
        int[]arr={1,1,2};
        List<List<Integer>> permutation = permutation(arr);
        System.out.println(permutation);
    }
    public static List<List<Integer>> permutation(int[] nums) {
        Arrays.sort(nums);
        dfs(nums);
        return list;
    }
    public static void dfs(int[] nums){
        if(list1.size()==nums.length){

            list.add(new ArrayList<Integer>(list1));
            return;
        }
        for (int i = 0; i < nums.length; i++) {
            if(i>0&&nums[i]==nums[i-1]&&!arr[i-1]){//理解为跳过从我开始而且我与前面一致因为前面一样的我已经全排列完了所以去重就可以直接跳过我
                continue;
            }
            if(!arr[i]){
                arr[i]=true;
                list1.add(nums[i]);
                dfs(nums);
                arr[i]=false;
                list1.remove(list1.size()-1);
            }
        }
    }
}
